﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class RateForm : Form
    {
        string client = "";
        public RateForm()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tabControl1.Enabled = true;
            btnView.Enabled = true;
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            this.Refresh();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
           
        }

        private void SelectFromCredit(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            selRate = dbWork.SelectFromCreditRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0) { dataGridView1.RowCount = 1; dataGridView1[0, 0].Value = ""; dataGridView1[1, 0].Value = ""; dataGridView1[2, 0].Value = ""; } else { dataGridView1.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selRate.title[j].ToString());
                dataGridView1[1, j].Value = selRate.procent[j].ToString();
                dataGridView1[2, j].Value = selRate.period[j].ToString();
                j++;
            }
            
        }

        private void SelectFromAccumulation(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            selRate = dbWork.SelectFromAccumulationRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0) { dataGridView2.RowCount = 1; dataGridView2[0, 0].Value = ""; dataGridView2[1, 0].Value = ""; dataGridView2[2, 0].Value = ""; } else { dataGridView2.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView2[0, j].Value = selRate.title[j].ToString();
                dataGridView2[1, j].Value = selRate.procent[j].ToString();
                dataGridView2[2, j].Value = selRate.period[j].ToString();
                j++;
            }
        }

       
        public void Refresh()
        {
            
            switch (comboBoxClient.SelectedIndex)
            {
                case 0:
                    client = "физический";
                    this.SelectFromCredit(client);
                    this.SelectFromAccumulation(client);
                    break;
                case 1:
                    client = "юридический";
                    this.SelectFromCredit(client);
                    this.SelectFromAccumulation(client);
                    break;
                case 2:
                    client = "банк";
                    this.SelectFromCredit(client);
                    this.SelectFromAccumulation(client);
                    break;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddRate addFr = new AddRate(this);
            addFr.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                EditRateForm editFR;
                int index;
                int id_rate;
                int procent;
                int period;
                string title;
                string operation;
                //string client;


                if (tabControl1.SelectedIndex == 0)
                {
                    DBWorkClass dbWork = new DBWorkClass();
                    ClassRateType selRate = new ClassRateType();
                    selRate = dbWork.SelectFromCreditRate(client);
                    index = dataGridView1.CurrentRow.Index;
                    id_rate = Convert.ToInt32(selRate.id_rate[index]);
                    procent = Convert.ToInt32(selRate.procent[index]);
                    period = Convert.ToInt32(selRate.period[index]);
                    title = selRate.title[index].ToString();
                    //client = comboBoxClient.SelectedText;
                    operation = "кредит";
                    editFR = new EditRateForm(tabControl1.SelectedIndex, operation, client, id_rate, title, procent, period, this);
                }
                else
                {
                    DBWorkClass dbWork = new DBWorkClass();
                    ClassRateType selRate = new ClassRateType();
                    selRate = dbWork.SelectFromAccumulationRate(client);
                    index = dataGridView2.CurrentRow.Index;
                    id_rate = Convert.ToInt32(selRate.id_rate[index]);
                    procent = Convert.ToInt32(selRate.procent[index]);
                    period = Convert.ToInt32(selRate.period[index]);
                    title = selRate.title[index].ToString();
                    //client = comboBoxClient.SelectedText;
                    operation = "счёт";
                    editFR = new EditRateForm(tabControl1.TabIndex, operation, client, id_rate, title, procent, period, this);
                }
                editFR.Show();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DBWorkClass dbWork = new DBWorkClass();
                ClassRateType selRate = new ClassRateType();
                if (tabControl1.SelectedIndex == 0)
                {
                    selRate = dbWork.SelectFromCreditRate(client);
                    int index = dataGridView1.CurrentRow.Index;
                    int id = Convert.ToInt32(selRate.id_rate[index]);
                    dbWork.DeleteFromRate(id, tabControl1.SelectedIndex);
                }
                else
                {
                    selRate = dbWork.SelectFromAccumulationRate(client);
                    int index = dataGridView2.CurrentRow.Index;
                    int id = Convert.ToInt32(selRate.id_rate[index]);
                    dbWork.DeleteFromRate(id, tabControl1.SelectedIndex);
                }
                this.Refresh();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }
    }
}
